import axios, {
	type AxiosResponse,
	type AxiosResponseHeaders,
	type InternalAxiosRequestConfig,
	type RawAxiosResponseHeaders,
} from "axios";
import type { axiosResponseDataCustomType } from "./interface/utils";
/**
 * @description  开发环境axios
 */
const axiosDevelopment = axios.create({
	baseURL: "",
	timeout: 3000,
});

/**
 * @description  生产环境axios
 */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const axiosProduct = axios.create({
	baseURL: "",
	timeout: 3000,
});
axiosProduct.interceptors.response.use(
	(res) => {
		// 业务逻辑成功，返回响应数据，作为axios成功的结果
		return res.data;
	},
	(err) => {
		return Promise.reject(err);
	}
);

const request = {
	/**
	 *
	 * @param data 传输需要传输的参数
	 * @param header 传输请求头
	 * @returns
	 */
	// 添加类型，支持泛型接口
	get: function <T>(url : string, header = {}) {
		// '1112'.startsWith
		const token = localStorage.getItem("token"); //如果有token就加上token
		let lang = localStorage.getItem("lang") || "zh-CN";
		// 截取只要碰到'zh'就对应后端的中文简体(后端定义什么就是什么)
		if (lang.startsWith("zh")) lang = "zhCN";
		return new Promise<axiosResponseDataCustomType<T>>((resolve, reject) => {
			axiosDevelopment
				.get<
					axiosResponseDataCustomType<T>,
					{
						data : axiosResponseDataCustomType<T>;
						status : number;
						statusText : string;
						headers : RawAxiosResponseHeaders | AxiosResponseHeaders;
						request ?: any;
					}
				>(url, {
					headers: {
						Authorization: "Bearer " + token,
						"Accept-Language": lang,
						...header,
					},
				})
				.then((data) => {
					const { status: statusCode, data: resData } = data;
					switch (statusCode) {
						case 200:
							console.debug("请求成功的动作");
							break;
						default:
							console.log("默认的动作");
					}
					if (resData) {
						resolve(resData); //返回数据
					}
				})
				.catch((error) => {
					console.error(error, "axios前端工具层错误");
					reject(error);
				});
		});
	},
	/**
	 *
	 * @param data 传输需要传输的参数
	 * @param header 传输请求头
	 * @returns
	 */
	post: function <T>(url : string, data = {}, header = {}) {
		const token = localStorage.getItem("token"); //如果有token就加上token
		let lang = localStorage.getItem("lang") || "zh-CN";
		// 截取只要碰到'zh'就对应后端的中文简体(后端定义什么就是什么)
		if (lang.startsWith("zh")) lang = "zhCN";
		return new Promise<axiosResponseDataCustomType<T>>((resolve, reject) => {
			axiosDevelopment
				.post<
					axiosResponseDataCustomType<T>,
					{
						data : axiosResponseDataCustomType<T>;
						status : number;
						statusText : string;
						headers : RawAxiosResponseHeaders | AxiosResponseHeaders;
						request ?: any;
					}
				>(url, data, {
					headers: {
						Authorization: "Bearer " + token,
						"Accept-Language": lang,
						...header,
					},
				})
				.then((data) => {
					const { status: statusCode, data: resData } = data;
					switch (statusCode) {
						case 200:
							console.debug("请求成功的动作");
							break;
						default:
							console.log("默认的动作");
					}
					if (resData) {
						resolve(resData); //返回数据
					}
				})
				.catch((error) => {
					console.error(error, "axios前端工具层错误");
					reject(error);
				});
		});
	},
};

export { request };